package com.tdr.myandroid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.tdr.myandroid.model.EventObject;
import com.tdr.myandroid.model.EventProtocol;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lemingdeng on 2014-07-17.
 */
public class EventArrayAdapter extends ArrayAdapter<EventProtocol> {
    private ArrayList<EventProtocol> eventArray;
    public EventArrayAdapter(Context context, List<EventProtocol> objects) {
        super(context, R.layout.event_list_item, objects);
        this.eventArray = (ArrayList<EventProtocol>)objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.event_list_item, null);

        EventProtocol event = eventArray.get(position);

        if (event != null) {
            TextView tv = (TextView)v.findViewById(R.id.eventText);
            tv.setText(event.getDescription());

            ToggleButton tb = (ToggleButton)v.findViewById(R.id.toggleButton);
            tb.setChecked(event.isActive());
            tb.setId(position);
            tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    eventArray.get(compoundButton.getId()).activate(b);
                }
            });
        }
        return v;
    }
}
